package com.mysgr.utils.id;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.eova.common.utils.xx;
import com.eova.common.utils.time.DateUtil;
import com.mysgr.model.SysSequence;
import com.mysgr.utils.DicCons;





/**
 * ID生成服务
 * 
 * <p>
 * ！待解决。这个组件混淆后的jar丢失路径元信息而无法被扫描。目前使用XML配置方式加载。
 * 
 * @author OSWorks-XC
 * @date 2014-06-01
 */
public class IdService {

	
	
	/**
	 * 生成ID
	 * 
	 * @param idname
	 * @return
	 * @throws Exception 
	 */
	public synchronized String createId(String idname) throws Exception {
		String id = "";

		SysSequence Seq = SysSequence.dao.findFirst("select * from sys_sequence where name =?", idname);
		if (xx.isEmpty(Seq)) {
			throw new Exception("Seq is null");
		}
		// 类型
		String type_ = Seq.getType();
		if (StringUtils.equals(type_, DicCons.SEQUENCE_TYPE_UUID)) {
			// UUID
			id = SgrId.uuid();
		} else if (StringUtils.equals(type_, DicCons.SEQUENCE_TYPE_APPID)) {
			// AppSequence
			Long idLong = Long.valueOf(Seq.getCurValue());
			idLong = idLong + 1; // 将当前值+1做为本次ID值
			if (idLong > new Long(Seq.getMaxValue())) {
				// 号源用完
				if (StringUtils.equals(Seq.getIsCircul(), DicCons.YES)) {
					// 从最小值开始循环
					idLong = new Long(Seq.getMinValue());
				} else {
					// 溢出
					throw new Exception( Seq.getName());
				}
			}
			id = String.valueOf(idLong);
			// 补足
			if (StringUtils.equals(Seq.getIsLeftpad(), DicCons.YES)) {
				id = StringUtils.leftPad(id, Seq.getMaxValue().length(), "0");
			}
		} else if (StringUtils.equals(type_, DicCons.SEQUENCE_TYPE_DBSEQUENCE)) {
			// DBSequence
			id = createIdBasedDbSeq(Seq.getMaxValue());
		}
		Seq.setCurValue(id);
		// 格式化
		id = format(id, Seq);
		// 回写配置表
		Seq.setFormatValue(id);
		Seq.update();
		return id;
	}

	/**
	 * 基于数据库的sequence机制生成ID
	 * 
	 * @param sequenceName
	 *            数据库Sequence的name值
	 * @return
	 */
	public String createIdBasedDbSeq(String sequenceName) {
		// TODO 待实现
		return "";
	}

	/**
	 * ID格式化处理
	 * 
	 * @param id
	 * @param Seq
	 * @return
	 */
	private String format(String id, SysSequence Seq) {
		if (xx.isEmpty(id)) {
			return id;
		}
		String prefix = Seq.getPrefix();
		if (xx.isEmpty(prefix)) {
			prefix = "";
		} else {
			String aString = StringUtils.substringBefore(prefix, "{");
			String bString = StringUtils.substringBetween(prefix, "{", "}");
			if (bString != null) {
				bString = DateUtil.format(new Date(), bString);
			} else {
				bString = "";
			}
			String cString = StringUtils.substringAfter(prefix, "}");
			prefix = aString + bString + cString;
		}
		String suffix = Seq.getSuffix();
		if (xx.isEmpty(suffix)) {
			suffix = "";
		} else {
			String aString = StringUtils.substringBefore(suffix, "{");
			String bString = StringUtils.substringBetween(suffix, "{", "}");
			if (bString != null) {
				bString = DateUtil.format(new Date(), bString);
			} else {
				bString = "";
			}
			String cString = StringUtils.substringAfter(suffix, "}");
			suffix = aString + bString + cString;
		}
		String linkString = Seq.getConnector() == null ? "" : Seq.getConnector();
		id = prefix + linkString + id;
		if (xx.isEmpty(suffix)==false) {
			id = id + linkString + suffix;
		}
		return id;
	}


}
